package dbmanager;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;


/**
 * Trieda pre pripojenie a odpojenie do/z Oracle MSSQL.
 * @author PeterCagan
 * 
 */
public class MSSQL implements Database{
	
	private static final String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	private static final String portNumber = "1433";
	private static final String menoDat = "Projekt";
	private static final String user = "Bratislava";
	private static final String password = "psdsd";
	
	private String url = null;
	private String serverName = null;
	
	
	/**
	 * Konstruktor pre vytvorenie instancie triedy MSSQL 
	 * @throws ClassNotFoundException 
	 */
	public MSSQL(String serverN){
		serverName = serverN;
		url = "jdbc:sqlserver://" + serverName + ":" + portNumber + ";databaseName=" + menoDat;
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			System.out.println("Nepodarilo sa ziskat db driver.");
		}
	}
	
	/**
	 * Pripoji do databazy. 
	 * @throws SQLException 
	 */
	public SQLManager connect() {
		Connection con = null;
		try {
			con = DriverManager.getConnection(url,user,password);
			System.out.println("Uspesne pripojenie na db.");
		} catch (SQLException e) {
			System.out.println("Nepodarilo sa pripojit na db.");
		}
		return new SQLManager(con);
	}
	
	/**
	 * Odpoji z databazy.
	 * @throws SQLException 
	 */
	public void disconnect(Connection con){
		if(con != null)
			try {
				con.close();
				System.out.println("Uspesne odpojene z db.");
				} catch (SQLException e) {	
					System.out.println("Nepodarilo sa uzavriet spojenie!");
					e.printStackTrace();
				}
		}
}